# -*- coding: utf8 -*-

import sys
import os
import time

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

from setting import *
from macro import *
from logger import LOG
from common import *
from fps import Fps
from draw_something import draw_ground
from camera_control import Camera
from mouse import pick, draw_test


def init_gl(width, height):
    global camera

    glClearColor(0.0, 0.0, 0.0, 0.0)  # This Will Clear The Background Color To Black
    glClearDepth(1.0)  # Enables Clearing Of The Depth Buffer
    glDepthFunc(GL_LESS)  # The Type Of Depth Test To Do
    glEnable(GL_DEPTH_TEST)  # Enables Depth Testing
    glShadeModel(GL_SMOOTH)  # Enables Smooth Color Shading
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()  # Reset The Projection Matrix

    # Calculate The Aspect Ratio Of The Window
    # fovy: 角度
    # aspect: 宽高比
    # zNear: 沿z轴方向的两裁面之间的距离的近处，
    gluPerspective(45.0, float(width) / float(height), 0.1, 100.0)

    glMatrixMode(GL_MODELVIEW)


def window_resize(width, height):
    # 防止部分除法出现异常
    if height == 0:
        height = 1

        glViewport(0, 0, width, height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45.0, float(width) / float(height), 0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        # glutReshapeWindow(WINDOW_WIDTH, WINDOW_HEIGHT)


def key_pressed(*args):
    """
    args: (key_code, x, y)
    """

    global camera
    LOG.info('key pressed: ' + str(args[0]))

    if args[0] == KEY_ESCAPE:
        sys.exit()

    if args[0] == 'w' or args[0] == GLUT_KEY_UP:
        camera.move_up()

    elif args[0] == 'a' or args[0] == GLUT_KEY_LEFT:
        camera.move_left()

    elif args[0] == 's' or args[0] == GLUT_KEY_DOWN:
        camera.move_down()

    elif args[0] == 'd' or args[0] == GLUT_KEY_RIGHT:
        camera.move_right()


def mouse_event(button, state, x, y):
    LOG.debug('mouse event')
    pick(button, state, x, y)


def draw():
    global fps, camera  # 告诉这个函数这两个变量是全局变量

    # 清除屏幕
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()  # 重置当前指定的矩阵为单位矩阵

    camera.update()
    glColor3f(1.0, 0, 0)
    glutWireTeapot(3)

    fps.show()
    draw_ground()
    draw_test(GL_RENDER)

    glutSwapBuffers()


if __name__ == "__main__":
    width = WINDOW_WIDTH
    height = WINDOW_HEIGHT

    # fps 显示控制
    fps = Fps(0, height - 18)

    # camera 视角控制
    camera = Camera()

    glutInit(sys.argv)  #

    # 双buffer， RGBA颜色，深度buffer
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)

    # 在createWindow之前初始化大小和位置
    glutInitWindowPosition(100, 40)
    glutInitWindowSize(width, height)

    window = glutCreateWindow("Roaming")

    glutDisplayFunc(draw)

    # glutFullScreen()

    # 处理窗口大小变化事件
    glutReshapeFunc(window_resize)

    glutIdleFunc(draw)

    # 键盘响应 和 鼠标事件响应
    glutMouseFunc(mouse_event)
    glutKeyboardFunc(key_pressed)
    glutSpecialFunc(key_pressed)

    init_gl(width, height)
    glutMainLoop()